package com.javatian.controller;

import com.github.pagehelper.PageInfo;
import com.javatian.common.Result;
import com.javatian.common.ResultCode;
import com.javatian.entity.Account;
import com.javatian.entity.CartInfo;
import com.javatian.entity.GoodsInfo;
import com.javatian.entity.OrderInfo;
import com.javatian.exception.CustomException;
import com.javatian.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 订单信息控制器
 */
@RestController
@RequestMapping(value = "/orderInfo")
public class OrderInfoController {

    //用户信息
    @Value("${const.user}")
    private String user;

    @Resource
    private OrderInfoService orderInfoService;

    /**
     * 下单功能
     */
    @PostMapping
    public Result<OrderInfo> add(@RequestBody OrderInfo orderInfo){
        Long userId = orderInfo.getUserId();
        List<GoodsInfo> goodsList = orderInfo.getGoodsList();
        if(userId == null || goodsList == null || goodsList.size() == 0){
            throw new CustomException(ResultCode.PARAM_ERROR);
        }
        return Result.success(orderInfoService.add(orderInfo));
    }

    /**
     * 根据条件分页查询
     * @param pageNum 第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @GetMapping("/page")
    public Result<PageInfo<OrderInfo>> page(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false,defaultValue = "5") Integer pageSize,
                                           HttpServletRequest request){
        Account account =(Account) request.getSession().getAttribute(user);
        return Result.success(orderInfoService.findEndPage(pageNum,pageSize,account));
    }

    /**
     * 根据条件分页查询
     * @param pageNum 第几页
     * @param pageSize 每页数量
     * @return 当前页的List列表
     */
    @GetMapping("/page/front")
    public Result<PageInfo<OrderInfo>> page(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                           @RequestParam(required = false,defaultValue = "5") Integer pageSize,
                                           @RequestParam(required = false)Long userId,
                                           @RequestParam(required = false)Integer level){
        return Result.success(orderInfoService.findFrontPage(pageNum,pageSize,userId,level));
    }

    /**
     * 修改订单状态
     */
    @PostMapping("/status/{id}/{status}")
    public Result status(@PathVariable Long id,@PathVariable String status){
        try {
            orderInfoService.changeStatus(id,status);
            return Result.success();
        }catch (Exception e){
            return Result.error("-1","账户余额不足");
        }


    }

    /**
     * 删除某个订单
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id){
        try {
            orderInfoService.delete(id);
            return Result.success();
        }catch (Exception e){
            return Result.error("-1","删除失败");
        }
    }
}






















